﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for ServiceLabwell
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class ServiceLabwell : System.Web.Services.WebService {

    static ClassExecute.ClassExecute dt;
    static string str = webapp4U.Configuration.CurrentConnectionString;
    static string error = "";

    public ServiceLabwell () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string InsertToXML(string Email)
    {
        try
        {
            if (File.Exists(Server.MapPath("~/XML/Mail.xml")))
            {
                //create new instance of XmlDocument
                XmlDocument doc = new XmlDocument();

                //load from file
                doc.Load(Server.MapPath("~/XML/Mail.xml"));


                //create node and add value
                XmlNode node = doc.CreateNode(XmlNodeType.Element, "row", null);
                //node.InnerText = "this is new node";
                //create att and add value
                XmlAttribute att = doc.CreateAttribute("MailAddress");
                att.Value = Email;
                node.Attributes.Append(att);

                //add to elements collection
                doc.DocumentElement.AppendChild(node);

                //save back
                doc.Save(Server.MapPath("~/XML/Mail.xml"));

            }
            else
            {
                XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/XML/Mail.xml"), Encoding.UTF8);
                writer.WriteStartDocument(true);

               // writer.WriteString("\n");

                //bắt đầu từ node cha
                writer.WriteStartElement("Mails");

               // writer.WriteString("\n");
                //vô node con
                writer.WriteStartElement("row");

                //vô thuộc tính đầu tiên,ghi dữ liệu và đóng lại
                writer.WriteStartAttribute("MailAddress");
                writer.WriteString(Email);
                writer.WriteEndAttribute();

                //kết thúc node con
                writer.WriteEndElement();
                //writer.WriteString("\n");
                //endbc
                writer.WriteEndElement(); //End of Root Element
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
            return "Xin cám ơn,chúng tôi sẽ liên hệ với quý khách";
        }
        catch
        {
            return "Hệ thống đang nâng cấp,xin quý khách khách đăng ký sau";
        }
    }
     [WebMethod(EnableSession = true)]
    public string CheckLogin(string UserName, string Pass)
    {
        dt = new ClassExecute.ClassExecute();
        string kq = dt.GetDataDepend(str, "k_checkLogin", CommandType.StoredProcedure, ref error,
            new SqlParameter("@UserName", UserName),
            new SqlParameter("@Password", dt.GetMd5Sum(Pass)));
        if (kq != "notauthentication")
        {
            //DataTable db = dt.Get_Data_DataTable(str, ref error, "k_GetMenuManagement", CommandType.StoredProcedure,
            // new SqlParameter("@TypeID", kq));
            if (HttpContext.Current.Session["module"] != null)
            {
                HttpContext.Current.Session["module"] = null;
            }
            if (HttpContext.Current.Session["user"] != null)
            {
                HttpContext.Current.Session["user"] = null;
            }
            //Session["module"] = db;
            HttpContext.Current.Session["user"] = UserName;
            HttpContext.Current.Session["UserType"] = kq;
            HttpContext.Current.Response.Redirect(webapp4U.Globals.ApplicationPath + "Page/ManagementSystem/Default.aspx", true);
            return "Xin chào mừng bạn đến với hệ thống";
        }
        else
        {
            return "Tài khoản đăng nhập không hợp lệ";
        }
    }
    
}
